<?php
/**
 * Created by PhpStorm.
 * User: MRPHAN
 * Date: 9/21/2014
 * Time: 8:37 PM
 */
namespace Backend\Modules\Video\Controllers;
 use Backend\Modules\Video\Forms\FormCreateArtist;
 use Backend\Modules\Video\Models\Artist ;
 use Phalcon\Paginator\Adapter\Model as Paginator;
 use Phalcon\Mvc\Model\Resultset;
 use Library\Pagination\Pager as Pager;
 class ArtistController extends \BackendController{
     public function indexAction(){


         $currentPage = abs($this->request->getQuery('page', 'int', 1));
         $artists = Artist::find();
         
         if ($currentPage == 0) {
             $currentPage = 1;
         }

         $pager = new Pager(
             new Paginator(array(
                 'data'  => $artists,
                 'limit' => 5,
                 'page'  => $currentPage,
             )),
             array(
                 // We will use Bootstrap framework styles
                 'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                 // Range window will be 5 pages
                 'rangeLength' => 5,
                 // Just a string with URL mask
                 'urlMask'     => 'http://localhost/javhd/javhd-admin/video/artist?page={%page_number}',
                 // Or something like this
                 // 'urlMask'     => sprintf(
                 //     '%s?page={%%page_number}',
                 //     $this->url->get(array(
                 //         'for'        => 'index:posts',
                 //         'controller' => 'index',
                 //         'action'     => 'index'
                 //     ))
                 // ),
             )
         );

         $this->view->pager = $pager;
     }

     public function  createAction(){
         $this->assets
             ->addCss('vendor/bootstrap/dist/css/bootstrap-datepicker.css');
         $this->assets
             ->addJs('vendor/bootstrap/dist/js/bootstrap-datepicker.js');
         $this->assets
             ->addJs('vendor/backend/js/video/artist.js');

         $this->assets
             ->addCss('vendor/jquery-file-upload/css/jquery.fileupload.css')
             ->addCss('vendor/jquery-file-upload/css/jquery.fileupload-ui.css')

             ->addJs('vendor/jquery-file-upload/js/vendor/jquery.ui.widget.js')
             ->addJs('http://blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js')
             ->addJs('http://blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js')
             ->addJs('vendor/jquery-file-upload/js/jquery.iframe-transport.js')
             ->addJs('vendor/jquery-file-upload/js/jquery.fileupload.js')
             ->addJs('vendor/jquery-file-upload/js/jquery.fileupload-process.js')
             ->addJs('vendor/jquery-file-upload/js/jquery.fileupload-image.js')
             ->addJs('vendor/jquery-file-upload/js/main.js');

         $form = new FormCreateArtist();

         if($this->request->getPost())
         {
            if($form->isValid($this->request->getPost()) !=  false)
            {
                $artist = new Artist();
                $artist->assign(array(
                    'artist_type'      => $this->request->getPost('artist_type'),
                    'name'             => $this->request->getPost('name'),
                    'national_id'      => $this->request->getPost('national_id'),
                    'gender'           => $this->request->getPost('gender'),
                    'info'             => $this->request->getPost('info'),
                    'bod'              => date( 'Y-m-d', strtotime( $this->request->getPost('bod') ) )
                ));
                if($artist->save())
                {
                    return $this->dispatcher->forward(array(
                        'controller' => 'artist',
                        'action' => 'index'
                    ));
                }
                else
                {
                    $this->flash->error($artist->getMessages());
                }
            }
         }

         $this->view->form = $form;
     }

     public function editAction($id){
         $this->assets
             ->addCss('layouts/skins/backend/defaults/css/bootstrap-datepicker.css');
         $this->assets
             ->addJs('layouts/skins/backend/defaults/js/bootstrap-datepicker.js');
         $this->assets
             ->addJs('vendor/backend/js/artist.js');

         $artist = $this->getFindFirstById($id);
         if(!$artist)
         {
             $this->flash->error("Arist was not found");
             return $this->dispatcher->forward(array(
                 'action' => 'index'
             ));
         }
         $form = new FormCreateArtist($artist , array('edit' => true));

         if($this->request->getPost())
         {
             if($form->isValid($this->request->getPost()) != false)
             {
                 $artist->assign(array(
                     'artist_type'      => $this->request->getPost('artist_type'),
                     'name'             => $this->request->getPost('name'),
                     'national_id'      => $this->request->getPost('national_id'),
                     'gender'           => $this->request->getPost('gender'),
                     'info'             => $this->request->getPost('info'),
                     'bod'              => $this->request->getPost('bod')
                 ));

                 if($artist->save())
                 {
                     return $this->dispatcher->forward(array(
                         'action' => 'index'
                     ));
                 }
                 $this->flash->error($artist->getMessages());
             }
         }
         $this->view->form = $form;
     }
     public function deleteAction($id)
     {
         $artist = $this->getFindFirstById($id);
         if(!$artist)
         {
             $this->flash->error("Video was not found");
             return $this->dispatcher->forward(array(
                 'action' => 'index'
             ));
         }
         if(!$artist->delete())
         {
             $this->flash->error($artist->getMessages());
         }
         else
         {
             $this->flash->success("Artist was deleted");
         }

         return $this->dispatcher->forward(array(
             'action' => 'index'
         ));
     }
     public  function getFindFirstById($id){
         return Artist::findFirstById($id);
     }
 }